##################################################
## Replication materials for 
## "Competent legislators or mere pawns? 
## Experimental evidence of attitudes toward gender quota politicians"
## by Carolyn Barnett, Alexandra Blackman, and Marwa Shalaby

## 2. Main Manuscript Tables and Figures 
## This file creates all tables and figures in the published manuscript

## **NOTE: Run file "01_setup.R" first to load necessary packages, functions, and data**

# Additional Set-Up ---------------------------

# NOTE: morvign_long should be re-created at the beginning of each code file
# to avoid errors

# Transform dataset to long format 
morvign_long <- gather(morvign, measure, value, 
                       c(vignette_competence:vignette_pawnlike), 
                       factor_key=TRUE)

# Recode variables
morvign_long$vignette_election <- factor(morvign_long$vignette_election)
morvign_long$vignette_mode <- factor(morvign_long$vignette_mode)
morvign_long$vignette_work <- factor(morvign_long$vignette_work)
morvign_long$measure <- factor(morvign_long$measure, levels= c("vignette_competence", 
                                                               "vignette_pawnlike"))

# FIGURE 1: Effect of politician gender and mode of election on competency and pawn-like measures ------------------

vign_election_means <- summarySE(morvign_long, measurevar="value", 
                                 groupvars=c("measure","vignette_election"), na.rm= T)
vign_election_means

vign_election_main <- vign_election_means %>% 
  filter(!is.na(vignette_election))


##### CREATE MAIN OUTCOMES GRAPH (FIGURE 1)

ggplot(vign_election_main, aes(x=measure, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome mean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.1)) +
  scale_x_discrete(labels=c("Competence","Pawnlike")) +
  labs(x = "Outcome measures", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_main.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 7.5, height = 4.5, units = "in", dpi =300)


# TABLE 1: Difference in Means: Politician Gender and Election Mode -------------

# Create just women dataset
morvign_women <- morvign %>% filter(vignette_gender == "Woman")

# create tidy table with t-tests for variables of interest
morvign_women_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ vignette_mode, morvign_women)),
                                   tidy(t.test(vignette_pawnlike ~ vignette_mode, morvign_women))
)

# prepare table for latex estimate1 = general women & estimate2 = quota women
morvign_women_t_tests <- morvign_women_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Variable" = variable,
                "Mean (General Woman)" = estimate1,
                "Mean (Quota Woman)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

# Create just quota women/general men dataset
morvign_men <- morvign %>% 
  filter(vignette_election == "Man/general" | vignette_election == "Woman/quota")

# create tidy table with t-tests for variables of interest
morvign_men_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ vignette_mode, morvign_men)),
                                 tidy(t.test(vignette_pawnlike ~ vignette_mode, morvign_men))
)

# prepare table for latex estimate1 = general men & estimate2 = quota women
morvign_men_t_tests <- morvign_men_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Variable" = variable,
                "Mean (General Man)" = estimate1,
                "Mean (Quota Woman)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

# Print data for Table 1
# NOTE: separate tables printed here combined into Table 1 manually in manuscript
# NOTE: p-values calculated here are used in FDR analysis in "06_robustness.R"
stargazer(morvign_women_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)

stargazer(morvign_men_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)



# FIGURE 2: Effect of collaboration type on competency and pawn-like measures, Quota women only ---------------
# dataset of only quota women
vign_quota_collab <- morvign_long %>% 
  filter(!is.na(vignette_election)) %>%
  filter(vignette_election == "Woman/quota")

vign_quota_collab_means <- summarySE(vign_quota_collab, 
                                     measurevar="value", 
                                     groupvars=c("measure","vignette_work"), na.rm= T)
vign_quota_collab_means

vign_quota_collab_means <- vign_quota_collab_means %>% 
  filter(!is.na(vignette_work))

##### CREATE GRAPH OF COLLABORATION TYPE (QUOTA WOMEN ONLY)

ggplot(vign_quota_collab_means, aes(x=measure, y=value, fill=vignette_work)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "grey70")+
  ylab("Outcome mean") + 
  scale_fill_manual(values = c("#999999", "#333333")) + 
  coord_cartesian(ylim = c(4.1, 6.1)) +
  scale_x_discrete(labels=c("Competence","Pawnlike")) +
  labs(x = "Outcome measures", fill = "Type of Collaboration", 
       caption = "")

ggsave("vign_quota_collab_means.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 7.5, height = 4.5, units = "in", dpi =300)


# TABLE 2: Difference in Means: Type of Collaboration (Quota Women Only) ----------------

# Create just quota women dataset
morvign_wq <- morvign %>% filter(vignette_election=="Woman/quota")

# create tidy table with t-tests for variables of interest
morvign_wq_work_t_tests <- bind_rows(tidy(t.test(vignette_competence ~ vignette_work, morvign_wq)),
                                     tidy(t.test(vignette_pawnlike ~ vignette_work, morvign_wq))
)

# prepare table for latex estimate1 = with his/her party & estimate2 = with another party
morvign_wq_work_t_tests <- morvign_wq_work_t_tests %>%
  add_column(variable = c("Competence",
                          "Pawnlike")) %>%
  dplyr::select(variable,estimate1,estimate2,estimate,p.value) %>%
  mutate_at(vars(estimate1, estimate2, estimate), funs(round(., 3))) %>%
  mutate_at(vars(p.value), funs(round(., 3))) %>%
  mutate(estimate = -estimate) %>%
  dplyr::rename("Outcome" = variable,
                "Mean (With her party)" = estimate1,
                "Mean (With another party)" = estimate2,
                "Difference" = estimate,
                "p-value" = p.value)

# Print table
# NOTE: p-values calculated here are used in FDR analysis in "06_robustness.R"
stargazer(morvign_wq_work_t_tests,
          type="latex",
          summary = F,
          rownames = F,
          single.row = T)


# FIGURE 3: Competency and Pawn-Like Outcome Measures by Degree of Trust in Elections --------

vign_election_trust <- summarySE(morvign_long, 
                                 measurevar="value", 
                                 groupvars=c("measure","vignette_election",
                                             "trust_in_elections"), na.rm= T)

vign_election_trust <- vign_election_trust %>% 
  filter(!is.na(trust_in_elections))

vign_election_trust

##### CREATE GRAPH

ggplot(vign_election_trust, aes(x=trust_in_elections, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Trust in Elections", x = "Level of Trust in Elections", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_elec_trust.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)


# FIGURE 4: Competency and Pawn-Like Outcome Measure by Voter Status --------------

vign_election_voter <- summarySE(morvign_long, 
                                 measurevar="value", 
                                 groupvars=c("measure","vignette_election","Voted"), na.rm= T)

vign_election_voter <- vign_election_voter %>% filter(!is.na(Voted))

vign_election_voter

##### CREATE GRAPH

ggplot(vign_election_voter, aes(x=Voted, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome\nmean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.4)) +
  facet_wrap(~measure, nrow = 1, labeller = outcome_names) +
  labs(title="By Voter History", x = "Voter History", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_voted.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 5.5, units = "in", dpi =300)

